package com.prs.crm.action.oa.report;

import java.util.Collection;
import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.action.ReportAction;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.Apply;
import com.prs.crm.report.ChartResult;
import com.prs.crm.report.ChartService;
import com.prs.crm.report.ExportConfig;
import com.prs.crm.report.ReportCell;
import com.prs.crm.report.ReportTable;
import com.prs.crm.report.ReportUtils;
import com.prs.crm.service.oa.ApplyService;

public class ApplyReportAction extends ReportAction {
	private static final long serialVersionUID = 2422942273677007655L;

	private ApplyService applyService;
	private ChartResult chartResult;
	private ChartService chartService;
	private PaginationSupport pagination;
	private Person person;
	private String category;
	private int exportType;

	private Integer state;
	private String level;
	private Date datestart;
	private Date dateend;
	private String acceptnames;
	private String accepts;

	@Action("editApplyReport")
	public String editApplyReport() {
		return SUCCESS;
	}

	@Action(value = "viewApplyPie", results = {
			@Result(name = "success", type = "chain", location = "editApplyReport"),
			@Result(name = "error", type = "chain", location = "editApplyReport") })
	public String viewApplyPie() {
		if (state == null) {
			addActionError("状态不能为空");
			return ERROR;
		}
		if (level == null || "".equals(level.trim())) {
			addActionError("级别不能为空");
			return ERROR;
		}
		if (datestart == null) {
			addActionError("开始时间不能为空");
			return ERROR;
		}
		if (dateend == null) {
			addActionError("结束时间不能为空");
			return ERROR;
		}
		if (accepts == null || "".equals(accepts.trim())) {
			addActionError("人员不能为空");
			return ERROR;
		}
		Collection<Person> persons = !accepts.trim().equals("") ? chartService
				.convertIdsToPersons(ToolKits.convertStringAryToIntAry(accepts
						.split(","))) : null;
		Collection<?> dataList = this.getApplyService().getReportApplySource(
				state == 5 ? null : state, level, datestart, dateend, persons);
		chartResult = this.getChartService().export(
				this.getApplyService().fillDataset(dataList,
						Constrains.REPORT_PIECHART), "公务统计分析",
				"listReportApply");
		return SUCCESS;
	}

	@Action(value = "viewApplyBar", results = {
			@Result(name = "success", type = "chain", location = "editApplyReport"),
			@Result(name = "error", type = "chain", location = "editApplyReport") })
	public String viewApplyBar() {
		if (state == null) {
			addActionError("状态不能为空");
			return ERROR;
		}
		if (level == null || "".equals(level.trim())) {
			addActionError("级别不能为空");
			return ERROR;
		}
		if (datestart == null) {
			addActionError("开始时间不能为空");
			return ERROR;
		}
		if (dateend == null) {
			addActionError("结束时间不能为空");
			return ERROR;
		}
		if (accepts == null || "".equals(accepts.trim())) {
			addActionError("人员不能为空");
			return ERROR;
		}
		Collection<Person> persons = !accepts.trim().equals("") ? chartService
				.convertIdsToPersons(ToolKits.convertStringAryToIntAry(accepts
						.split(","))) : null;
		Collection<?> dataList = this.getApplyService().getReportApplySource(
				state == 5 ? null : state, level, datestart, dateend, persons);
		chartResult = this.getChartService().export(
				this.getApplyService().fillDataset(dataList,
						Constrains.REPORT_BARCHART), "公务统计分析", "数量", "",
				"listReportApply");
		return SUCCESS;
	}

	@Action("listReportApply")
	public String listReportApply() {
		category = ReportUtils.URLDecode(this.getCategory());
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Apply.class);
		Integer a = state == 5 ? null : state;

		if (a != null)
			criteria.add(Restrictions.eq("flowstatus", a));
		else
			criteria.add(Restrictions.isNull("flowstatus"));

		level = ReportUtils.URLDecode(level);
		if (level != null)
			criteria.add(Restrictions.eq("level", level));
		if (datestart != null)
			criteria.add(Restrictions.ge("recordTime", datestart));
		if (dateend != null)
			criteria.add(Restrictions.le("recordTime", dateend));
		if (category != null && category.trim().length() > 0) {
			criteria.createAlias("recorder", "re");
			criteria.createAlias("re.resume", "rs");
			criteria.add(Restrictions.eq("rs.name", category));
		}

		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getApplyService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action(value = "exportApplyReport", results = { @Result(name = "success", type = "stream", params = {
			"contentType", "${contentType}", "inputName", "inputStream",
			"bufferSize", "1024", "contentDisposition",
			"filename=${outfileName}" }) })
	public String exportApplyReport() {
		ReportTable table = new ReportTable("公务统计分析报表");
		table.addCell(new ReportCell("序号", "id"));
		table.addCell(new ReportCell("主题", "title"));
		table.addCell(new ReportCell("提交人", "recorder.resume.name"));
		table.addCell(new ReportCell("提交时间", "recordTime"));
		table.addCell(new ReportCell("级别", "level"));
		table.addCell(new ReportCell("状态", "flowstatus", Constrains
				.getReportProjectTaskStatus()));
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Apply.class);
		Integer a = state == 5 ? null : state;

		if (a != null)
			criteria.add(Restrictions.eq("flowstatus", a));
		else
			criteria.add(Restrictions.isNull("flowstatus"));

		if (level != null)
			criteria.add(Restrictions.eq("level", level));
		if (datestart != null)
			criteria.add(Restrictions.ge("recordTime", datestart));
		if (dateend != null)
			criteria.add(Restrictions.le("recordTime", dateend));
		if (category != null && category.trim().length() > 0) {
			criteria.createAlias("recorder", "re");
			criteria.createAlias("re.resume", "rs");
			criteria.add(Restrictions.eq("rs.name", category));
		}
		this.setOutFilePath(this.getReportService().report(criteria, table,
				new ExportConfig(), this.getExportType()));
		return SUCCESS;
	}

	public ApplyService getApplyService() {
		return applyService;
	}

	public void setApplyService(ApplyService applyService) {
		this.applyService = applyService;
	}

	public ChartResult getChartResult() {
		return chartResult;
	}

	public void setChartResult(ChartResult chartResult) {
		this.chartResult = chartResult;
	}

	public ChartService getChartService() {
		return chartService;
	}

	public void setChartService(ChartService chartService) {
		this.chartService = chartService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public int getExportType() {
		return exportType;
	}

	public void setExportType(int exportType) {
		this.exportType = exportType;
	}

	public Integer getState() {
		return state;
	}

	public void setState(Integer state) {
		this.state = state;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public Date getDatestart() {
		return datestart;
	}

	public void setDatestart(Date datestart) {
		this.datestart = datestart;
	}

	public Date getDateend() {
		return dateend;
	}

	public void setDateend(Date dateend) {
		this.dateend = dateend;
	}

	public String getAcceptnames() {
		return acceptnames;
	}

	public void setAcceptnames(String acceptnames) {
		this.acceptnames = acceptnames;
	}

	public String getAccepts() {
		return accepts;
	}

	public void setAccepts(String accepts) {
		this.accepts = accepts;
	}

}
